package com.aaa.service.impl;

import com.aaa.dao.UserDao;
import com.aaa.dto.AddressDto;
import com.aaa.dto.LoginDto;
import com.aaa.dto.UserMinInfo;
import com.aaa.pojo.DefaultMsg;
import com.aaa.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Random;

@Service
@Transactional
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;

    @Override
    public DefaultMsg login(String username, String password) {
        LoginDto loginDto = new LoginDto();
        loginDto.setPassword(password);
        if (username.matches("^1[3-9]\\d{9}$")) {
            loginDto.setPhone(username);
        } else if (username.matches("^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,6}$")) {
            loginDto.setEmail(username);
        }

        DefaultMsg defaultMsg = new DefaultMsg(200, 0, null, null);
        LoginDto login = userDao.login(loginDto);

        // 如果查询到了 进入该if
        if (login != null) {
            defaultMsg.setMain(login);
            defaultMsg.setMsg(1);
        }

        return defaultMsg;
    }

    @Override
    public DefaultMsg register(String username, String password) {
        DefaultMsg defaultMsg = new DefaultMsg(200, 0, null, null);
        // 如果是 手机号 则 添加手机号用户详情
        // 这里可以添加默认用户名

        final String CHARACTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
        final int RANDOM_PART_LENGTH = 7; // 随机部分的长度

        Random random = new Random();
        StringBuilder nickname = new StringBuilder("uu_"); // 固定前缀

        for (int i = 0; i < RANDOM_PART_LENGTH; i++) {
            int index = random.nextInt(CHARACTERS.length());
            nickname.append(CHARACTERS.charAt(index));
        }

        LoginDto loginDto = new LoginDto();
        int i = 0;
        loginDto.setUsername(nickname.toString());
        loginDto.setPassword(password);
        int i1 = userDao.registerForUser(loginDto);
        if (i1 != 0) i++;
        if (username.matches("^1[3-9]\\d{9}$")) {
            loginDto.setPhone(username);
            int i2 = userDao.registerByPhoneForUserDetail(loginDto);
            if (i2 != 0) i++;
        } else if (username.matches("^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,6}$")) {
            loginDto.setEmail(username);
            int i2 = userDao.registerByEmailForUserDetail(loginDto);
            if (i2 != 0) i++;
        }
        if (i == 2) {
            defaultMsg.setMsg(1);
            // 返回了生成的用户的id
            defaultMsg.setMain(loginDto.getId());
        }
        return defaultMsg;
    }

    @Override
    public DefaultMsg findUser(String username) {
        DefaultMsg defaultMsg = new DefaultMsg(200, 1, null, null);
        LoginDto user = userDao.findUser(username);
        // 如果 找到了则 msg=0 并且返回用户id
        System.out.println(user == null);
        if (user != null) {
            defaultMsg.setMsg(0);
            defaultMsg.setMain(user.getId());
        }
        return defaultMsg;
    }

    @Override
    public List<AddressDto> findAddressByUserId(Integer userId) {
        return userDao.findAddressByUserId(userId);
    }

    @Override
    public DefaultMsg updateMinUserInfo(UserMinInfo userMinInfo) {
        DefaultMsg defaultMsg = new DefaultMsg(200, 1, null, null);
        int i1 = userDao.updateMinUserInfoDetail(userMinInfo);
        int i2 = userDao.updateMinUserInfo(userMinInfo);
        if (i1 == 0 || i2 == 0) defaultMsg.setMsg(0);
        return defaultMsg;
    }
}
